package com.mogist.hqc.module.main.presenter

import com.google.gson.reflect.TypeToken
import com.mogist.hqc.entitys.BaseResponse
import com.mogist.hqc.entitys.response.*
import com.mogist.hqc.module.main.contract.MainContract
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.mvp._PresenterImpl
import com.mogist.hqc.util.Constant
import talex.zsw.basecore.util.JsonTool

/**
 * 作用：首页
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class MainPresenter(view: MainContract.View) : _PresenterImpl<MainContract.View, BaseResponse>(view), MainContract.Presenter
{
    override fun requestSuccess(response: BaseResponse, http: HttpDto)
    {
        when (http.url)
        {
            Constant.MAIN_ROUTE     ->
            {
                if (response.success)
                {
                    val vo = JsonTool.getObject(response.content, MainRouteListVo::class.java)
                    mView.bindRoute(vo)
                }
                else
                {
                    mView.mainListError(0)
                }
            }
            Constant.MAIN_COMMUNITY ->
            {
                if (response.success)
                {
                    val vo = JsonTool.getObject(response.content, CommunityListVo::class.java)
                    mView.bindCommunity(vo)
                }
                else
                {
                    mView.mainListError(1)
                }
            }
            Constant.MAIN_PRODUCT ->
            {
                if (response.success)
                {
                    val vo = JsonTool.getObject(response.content, MainProductListVo::class.java)
                    mView.bindProduct(vo)
                }
                else
                {
                    mView.mainListError(2)
                }
            }
            Constant.TYPES        ->
            {
                if (response.success)
                {
                    val list =
                            JsonTool.getObject(response.content,
                                               object : TypeToken<ArrayList<MainTypeVo>>()
                                               {})
                    mView.bindTypes(list as ArrayList<MainTypeVo>?)
                }
            }
            Constant.TYPE_LIST    ->
            {
                if (response.success)
                {
                    val vo = JsonTool.getObject(response.content, MainTypeItemVo::class.java)
                    mView.initCard(vo, "")
                }
                else
                {
                    mView.initCard(null, response.message)
                }
            }
            Constant.REFRESH_TOKEN    ->
            {
                if (response.success)
                {
                    val vo = JsonTool.getObject(response.content, LoginVo::class.java)
                    mView.refreshSuccess(vo)
                }
            }
        }
    }

    override fun requestError(msg: String?, httpDto: HttpDto?)
    {
        when (httpDto!!.url)
        {
            Constant.MAIN_ROUTE     ->
            {
                mView.mainListError(0)
            }
            Constant.MAIN_COMMUNITY ->
            {
                mView.mainListError(1)
            }
            Constant.MAIN_PRODUCT   ->
            {
                mView.mainListError(2)
            }
            Constant.TYPE_LIST      ->
            {
                mView.initCard(null, "网络请求失败，请稍后重试！")
            }
        }
        super.requestError(msg, httpDto)
    }
}